<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="供应商" prop="supplierId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.supplierId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in supplierOptions" :key="item.id" :label="item.companyName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.cityId" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型" prop="bizTypes">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.bizTypes" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in bizTypeArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道" prop="channelIds">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.channelIds" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="平台" prop="platformIds">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.platformIds" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="车型" prop="vehicleTypeIds">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.vehicleTypeIds" placeholder="请选择" style="width: 256px">
            <el-option v-for="item in carTypeArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" @click="handleEdit('新增')">
          <template #icon><Plus /></template>
          新增
        </el-button>
        <el-button type="danger" :disabled="removeIds.length === 0" @click="handleDel()">
          <template #icon><Delete /></template>
          批量删除
        </el-button>
      </template>
      <el-table v-loading="loading" :data="TableData" highlight-current-row row-key="id" @selection-change="handleSelectionChange" @row-click="handleRowClick">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="供应商id" min-width="120" align="center" prop="id"></el-table-column>
        <el-table-column label="供应商" min-width="80" align="center" prop="supplierName"></el-table-column>
        <el-table-column label="业务类型" min-width="80" align="center" prop="bizType">
          <template #default="scope">
            <div style="display: flex; justify-content: center">
              <div>{{ bizTypeEnum[scope.row.bizType] }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="平台" min-width="80" align="center" prop="platformId">
          <template #default="scope">
            <div style="display: flex; justify-content: center">
              <div>{{ platFormEnum[scope.row.platformId] }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="渠道" min-width="80" align="center" prop="channelId">
          <template #default="scope">
            <div style="display: flex; justify-content: center">
              <div>{{ channelEnum[scope.row.channelId] }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="城市名称" min-width="80" align="center" prop="cityName"></el-table-column>
        <el-table-column label="车型" min-width="80" align="center" prop="vehicleTypeId">
          <template #default="scope">
            <div style="display: flex; justify-content: center">
              <div>{{ carTypeEnum[scope.row.vehicleTypeId] }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="派单比例:单位%" min-width="80" align="center" prop="percent"></el-table-column>
        <el-table-column fixed="right" align="center" label="操作" width="220">
          <template #default="scope">
            <div style="display: flex; justify-content: center; align-items: center">
              <el-button type="primary" link size="small" @click.stop="handleEdit('编辑', scope.row)">
                <template #icon><Pointer /></template>
                编辑
              </el-button>
              <el-button type="primary" link size="small" @click.stop="handleDel(scope.row.id)">
                <template #icon><Pointer /></template>
                删除
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <!-- 改派 -->
    <EditModal ref="editModalRef" @success="handleQuery" />
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "SysMenu",
  inheritAttrs: false,
});

import Supplier from "@/api/supplier";
import CommonApi from "@/api/common";
import EditModal from "./components/editModal.vue";
import {
  orderStatusEnum,
  carTypeEnum,
  carTypeArr,
  platFormEnum,
  channelEnum,
  orderTypeEnum,
  bizTypeEnum,
  luggageEnum,
  channelArr,
  bizTypeArr,
  orderTypeArr,
  platFormArr,
} from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";
import { log } from "console";

const queryFormRef = ref(ElForm);
const editModalRef = ref(null);
const total = ref(0);
const supplierOptions = ref();
const cityOptions = ref();

const loading = ref(false);

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);
const removeIds = ref([]);

// 选择表格的行菜单ID
const selectedMenuId = ref<number | undefined>();

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

function handleDel(id: any) {
  ElMessageBox.alert("确认删除吗?", {
    showCancelButton: true,
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    callback: (action) => {
      if (action == "confirm") {
        let data = {};
        if (id) {
          data.id = id;
        } else {
          data.ids = removeIds.value;
        }
        Supplier.supplierConfigDel(data).then((res) => {
          if (res.code == 0) {
            ElMessage.success(res.message);
            handleQuery();
          }
        });
      }
    },
  });
}
//编辑触发
function handleEdit(title: string, row: any) {
  editModalRef.value.edit(title, row);
}
// 查询菜单
function handleQuery() {
  loading.value = true;
  Supplier.supplierConfigList(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

// 行点击事件
function handleRowClick(row: MenuVO) {
  // 记录表格选择的菜单ID，新增子菜单作为父菜单ID
  selectedMenuId.value = row.id;
}

onMounted(() => {
  handleQuery();
  CommonApi.getSupplier({ pageNo: 1, pageSize: 9999, status: 1 }).then((data) => {
    if (data.code == 0) {
      supplierOptions.value = data.result;
    }
  });
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
